package com.grape.api.v1.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grape.annotation.OperateMethodName;
import com.grape.annotation.RepeatSubmit;
import com.grape.controller.BaseController;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import com.grape.vo.DisableVO;
import com.grape.vo.EnableVO;
import com.grape.domain.MemberGradePrice;
import com.grape.service.IMemberGradePriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 会员等级价格表 控制器
 * @author      putao
 * @email       2628279194@qq.com
 */
@RestController
@RequestMapping("memberGradePrices")
@Api(tags = "会员等级价格接口")
@AllArgsConstructor
public class MemberGradePriceController extends BaseController<MemberGradePrice> {

    private final IMemberGradePriceService memberGradePriceService;


    @GetMapping
    @ApiOperation(value = "会员等级价格分页")
    @OperateMethodName(value = "会员等级价格表列表分页", url = "member:memberGradePrices:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<MemberGradePrice> pageInfo = memberGradePriceService.pageList(baseVo);
        List<MemberGradePrice> memberGradePriceList = pageInfo.getList();
        return CollectionUtil.isNotEmpty(memberGradePriceList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @GetMapping("listPageCustom")
    @ApiOperation(value = "会员等级价格自定义分页")
    @OperateMethodName(value = "会员等级价格表列表", url = "member:memberGradePrices:listPageCustom")
    public R<?> pageListCustom(BaseVo baseVo) {
        Page<MemberGradePrice> pages = PageHelper.startPage(baseVo.getPageNum(), baseVo.getPageSize());
        memberGradePriceService.pageListCustom(baseVo);
        PageInfo<MemberGradePrice> pageInfo = pages.toPageInfo();
        return CollectionUtil.isNotEmpty(pageInfo.getList()) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @RepeatSubmit
    @PutMapping
    @ApiOperation(value = "会员等级价格修改")
    @OperateMethodName(value = "会员等级价格表修改", url = "member:memberGradePrices:update")
    public R<?> update(@Validated @RequestBody MemberGradePrice memberGradePrice) {
        if (!Strings.isNotBlank(memberGradePrice.getId())) {
            return updateNotFound(memberGradePrice, null);
        }
        memberGradePriceService.updateData(memberGradePrice);
        return updateSuccess(null, null);
    }


    @RepeatSubmit
    @PostMapping
    @ApiOperation(value = "添加会员等级价格")
    @OperateMethodName(value = "添加会员等级价格表", url = "member:memberGradePrices:insert")
    public R<?> insert(@Validated @RequestBody MemberGradePrice memberGradePrice) {
        return insertSuccess(memberGradePriceService.insertData(memberGradePrice), null);
    }


    @DeleteMapping
    @ApiOperation(value = "删除会员等级价格，可批量多个id逗号拼接")
    @OperateMethodName(value = "会员等级价格表删除,批量", url = "member:memberGradePrices:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空") @RequestParam String ids) {
        int number = memberGradePriceService.deleteData(ids);
        if (number < 1) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }


    @GetMapping("detail")
    @ApiOperation(value = "会员等级价格详情")
    @OperateMethodName(value = "会员等级价格表详情", url = "member:memberGradePrices:detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id) {
        MemberGradePrice memberGradePrice = memberGradePriceService.detail(id);
        if (memberGradePrice != null) {
            return selectSuccess(memberGradePrice, id);
        }
        return selectNotFound(id, null);
    }

    @PostMapping("disable")
    @ApiOperation(value = "禁用会员等级价格")
    @OperateMethodName(value = "禁用会员等级价格表", url = "member:memberGradePrices:disable")
    public R<?> disable(@Validated @RequestBody DisableVO disableVO) {
        memberGradePriceService.disable(disableVO);
        return updateSuccess(disableVO, null);
    }

    @PostMapping("enable")
    @ApiOperation(value = "启用会员等级价格")
    @OperateMethodName(value = "禁用会员等级价格表", url = "member:memberGradePrices:enable")
    public R<?> enable(@Validated @RequestBody EnableVO enableVO) {
        memberGradePriceService.enable(enableVO);
        return updateSuccess(enableVO, null);
    }
}

